package com.webex.ta.hydra.gui.util;

import javax.swing.*;
import javax.swing.event.EventListenerList;
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.MouseEvent;

/**
 * Created by Cisco WebEx.
 * User: vegaz
 * Date: 2010-10-20
 * Time: 14:04:20
 */
public class PlainIconButton extends JComponent {
    private boolean hovered = false;
    private boolean pressed = false;
    private boolean active = false;

    private CenteredIcon regularIcon, hoveredIcon, inactiveIcon;

    private EventListenerList listeners = new EventListenerList();

    public PlainIconButton(Icon icon) {
        setIcon(icon);
        this.setOpaque(false);
        setCursor(Cursor.getPredefinedCursor(Cursor.HAND_CURSOR));
        this.enableEvents(MouseEvent.MOUSE_EVENT_MASK | MouseEvent.MOUSE_MOTION_EVENT_MASK);
    }

    public void setIcon(Icon icon) {
        setIcons(icon, icon, icon);
    }

    public void setIcons(Icon regular, Icon hovered, Icon inactive) {
        int width = Math.max(regular.getIconWidth(), inactive.getIconWidth());
        width = Math.max(width, hovered.getIconWidth());
        int height = Math.max(regular.getIconHeight(), inactive.getIconHeight());
        height = Math.max(height, hovered.getIconHeight());

        setPreferredSize(new Dimension(width, height));

        regularIcon = new CenteredIcon(regular, width, height);
        hoveredIcon = new CenteredIcon(hovered, width, height);
        inactiveIcon = new CenteredIcon(inactive, width, height);
    }

    private Component getNearestOpaque() {
        Component c = this;
        while (c != null) {
            if (c.isOpaque()) return c;
            c = c.getParent();
        }
        return c;
    }

    public void addActionListener(ActionListener l) {
        listeners.add(ActionListener.class, l);
    }

    public void removeActionListener(ActionListener l) {
        listeners.remove(ActionListener.class, l);
    }

    private void fireActionPerformed() {
        ActionListener[] als = listeners.getListeners(ActionListener.class);
        for (ActionListener l : als) {
            l.actionPerformed(new ActionEvent(this, ActionEvent.ACTION_PERFORMED, ""));
        }
    }

    @Override
    protected void processMouseEvent(MouseEvent e) {
//        super.processMouseEvent(e);
        switch (e.getID()) {
            case MouseEvent.MOUSE_PRESSED:
                pressed = true;
                repaint();
                break;
            case MouseEvent.MOUSE_RELEASED:
                pressed = false;
                repaint();
                fireActionPerformed();
                break;
            case MouseEvent.MOUSE_ENTERED:
                hovered = true;
                repaint();
                break;
            case MouseEvent.MOUSE_EXITED:
                hovered = false;
                repaint();
                break;
        }
    }

    @Override
    protected void processMouseMotionEvent(MouseEvent e) {
//        super.processMouseMotionEvent(e);

    }

    protected void paintComponent(Graphics g) {
        super.paintComponent(g);
        if (hovered) {
            if (pressed) {
                hoveredIcon.paintIcon(this, g, 1, 1);
            } else {
                hoveredIcon.paintIcon(this, g, 0, 0);
            }
        } else {
            if (active) {
                regularIcon.paintIcon(this, g, 0, 0);
            } else {
                inactiveIcon.paintIcon(this, g, 0, 0);
            }
        }

//        g.translate(0, 0);
    }
}

